home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 401-425 / disk_416 / utils / du / du.a < prev    next >
Text File  |  1992-05-06  |  5KB  |  231 lines

  1. ;********************************************************************
  2. ;
  3. ; NAME        : Du v1.0
  4. ;
  5. ; TEMPLATE    : Du <dir name>,...
  6. ;
  7. ; DESCRIPTION    : print disk usage for a given set of directories
  8. ;
  9. ; AUTHOR    : Stuart Mitchell
  10. ;
  11. ; DATE        : 25-11-90
  12. ;
  13. ; NOTES     : 1) Du requires the ARP shared library (v39.0+)
  14. ;
  15. ;********************************************************************
  16.  
  17.         section du,code
  18.  
  19.         opt    o+
  20.  
  21.         incdir    "include:"
  22.  
  23.         include "libraries/arpbase.i"
  24.         include "libraries/dos.i"
  25.         include "exec/memory.i"
  26.  
  27. _LVOOpenLibrary     equ    -$228
  28. _LVOCloseLibrary    equ    -$19e
  29.  
  30. CALL        macro    (name)
  31.         jsr    _LVO\1(a6)
  32.         endm
  33.  
  34. r_anchor    equr    a2
  35. r_finfo     equr    a3
  36. r_current_arg    equr    a4
  37.  
  38. r_flags        equr    d1
  39. r_files     equr    d2
  40. r_dirs        equr    d3
  41. r_size        equr    d4
  42. r_blocks    equr    d5
  43. r_num_arg    equr    d6
  44. r_zero        equr    d7
  45.  
  46. ; bitdefs for dir scan - should really be picked up from ArpBase.i ...
  47.  
  48. DIDDIR        equ    3
  49. DODIR        equ    2
  50.  
  51. ;********************************************************************
  52.  
  53. start:        move.l    d0,d2            ; cache command line
  54.         move.l    a0,a2
  55.  
  56.         moveq.l #0,r_zero
  57.  
  58. ; open ARP library
  59.  
  60.         move.l    4.w,a6
  61.         lea    arplib(pc),a1
  62.         move.l    r_zero,d0
  63.         CALL    OpenLibrary
  64.         tst.l    d0
  65.         beq    exit
  66.         move.l    d0,a6
  67.  
  68. ; parse command line
  69.  
  70.         move.l    a2,a0            ; string
  71.         move.l    d2,d0            ; length
  72.         lea    help(pc),a1
  73.         lea    args(pc),a2             ; pointer to arg array
  74.         lea    tplate(pc),a3
  75.         CALL    GADS
  76.         move.l    d0,r_num_arg        ; count
  77.  
  78. ; allocate memory for AnchorPath structure - note freed automatically
  79.  
  80.         move.l    #ap_SIZEOF,d0
  81.         move.l    #MEMF_PUBLIC|MEMF_CLEAR,d1
  82.         CALL    ArpAllocMem
  83.         tst.l    d0
  84.         beq    closearp
  85.         move.l    d0,r_anchor
  86.  
  87. ; set up registers
  88.  
  89.         lea    ap_Info(r_anchor),r_finfo
  90.         move.l    args,r_current_arg    ; get base args
  91.  
  92. ; main loop
  93.  
  94. main_loop:    tst.l    r_num_arg        ; if count==0 then exit
  95.         beq    closearp
  96.  
  97. init_regs:    move.l    r_zero,r_files        ; zero results
  98.         move.l    r_zero,r_dirs
  99.         move.l    r_zero,r_size
  100.         move.l    r_zero,r_blocks
  101.  
  102.         move.b    #%00100001,ap_Flags(r_anchor)   ; DOWILD|DODOT
  103.  
  104. ; find first file/directory
  105.  
  106.         move.l    (r_current_arg),d0
  107.         move.l    r_anchor,a0
  108.         CALL    FindFirst
  109.         tst.l    d0
  110.         bne.s    no_files
  111.  
  112. ; loop around in directory until all subdirectories/files exhausted
  113. ; first check if CNTL-C pressed
  114.  
  115. du_loop:    move.l    r_zero,a1
  116.         CALL    CheckAbort
  117.         tst.l    d0
  118.         bne.s    break_pressed
  119.  
  120.         move.b    ap_Flags(r_anchor),r_flags
  121.  
  122. ; if DirEntryType <0 then we have a file
  123.  
  124.         move.l    fib_DirEntryType(r_finfo),d0
  125.         blt.s    do_file
  126.  
  127. do_dir:     btst    #DIDDIR,r_flags        ; DIDDIR bit set
  128.         beq.s    enter_dir        ; no ... enter dir
  129.  
  130. ; have just popped out of a directory, mark as finished & increment count
  131.  
  132. exit_dir:    bclr    #DIDDIR,r_flags
  133.  
  134.         addq.l    #1,r_dirs
  135.         bra.s    find_next
  136.  
  137. ; found a new directory, so enter it at next FindNext
  138.  
  139. enter_dir:    bset    #DODIR,r_flags
  140.         bra.s    find_next
  141.  
  142. ; this ones a file, so increment count & size
  143.  
  144. do_file:    addq.l    #1,r_files
  145.         add.l    fib_Size(r_finfo),r_size
  146.         add.l    fib_NumBlocks(r_finfo),r_blocks
  147.  
  148. ; find next file
  149.  
  150. find_next:    move.b    r_flags,ap_Flags(r_anchor)   ; set directory handling
  151.  
  152.         move.l    r_anchor,a0
  153.         CALL    FindNext
  154.         tst.l    d0
  155.         beq.s    du_loop         ; if zero then got something
  156.  
  157. ; directory exhausted
  158.  
  159.         bsr.s    free_anchor        ; free mem
  160.  
  161. ; print out results
  162.  
  163. print:        movem.l r_files/r_dirs/r_size/r_blocks,files     ; flush results
  164.  
  165.         move.l    (r_current_arg)+,a0     ; print directory name
  166.         CALL    Printf
  167.         lea    fmt(pc),a0        ; format string
  168.         lea    files(pc),a1        ; address ptrs to args
  169.         CALL    Printf            ; print results
  170.  
  171. ; and do it all again
  172.  
  173. end_loop:    subq.l    #1,r_num_arg        ; decrement arg count
  174.         bra.s    main_loop        ; and do next arg
  175.  
  176. ;********************************************************************
  177. ; handle case when directory/file doesn't exist...
  178.  
  179. no_files:    move.l    (r_current_arg)+,a0
  180.         CALL    Printf
  181.         lea    not_exist(pc),a1
  182.         CALL    Puts
  183.         bsr.s    free_anchor        ; free mem
  184.         bra.s   end_loop
  185.  
  186. ;********************************************************************
  187. ; called if CNTL-C detected
  188. ; NB: 1    - call to CheckAbort sets up a1 to point to break string
  189. ;     2 - drop through to end of program
  190.  
  191. break_pressed:    CALL    Puts            ; output "***BREAK"
  192.         bsr.s    free_anchor        ; free mem
  193.  
  194. ;*******************************************************************
  195. ; exit program - close ARP & return to DOS - call frees resources
  196.  
  197. closearp:    move.l    a6,a1
  198.         move.l    4.w,a6
  199.         CALL    CloseLibrary
  200.  
  201. exit:        move.l    r_zero,d0
  202.         rts
  203.  
  204. ;********************************************************************
  205. ; free anchorpath
  206.  
  207. free_anchor:    move.l    r_anchor,a0        ; free anchor
  208.         jmp    _LVOFreeAnchorChain(a6)
  209.  
  210. ;********************************************************************
  211. ; data
  212.  
  213. files        dc.l    0
  214. dirs        dc.l    0
  215. size        dc.l    0
  216. blocks        dc.l    0
  217.         even
  218. fmt        dc.b    " -- %ld files, %ld directories : %ld bytes (%ld blocks)",$0a,0
  219.         even
  220. arplib        dc.b    "arp.library",0
  221.         even
  222. args        dc.l    0            ; char *args[]
  223.         even
  224. help        dc.b    "<directory name> [...]",0
  225.         even
  226. tplate        dc.b    "DIR/...",0
  227.         even
  228. not_exist    dc.b    " -- directory does not exist",0
  229.         end
  230.  
  231.